home *** CD-ROM | disk | FTP | other *** search
- Path: ub239.dialup.uwa.edu.au!localhost!prye
- From: prye@cyllene.uwa.edu.au (Peter Rye)
- Newsgroups: comp.lang.c++
- Subject: Re: casting a void pointer back to a function pointer
- Date: 04 Apr 1996 17:42:22 GMT
- Organization: The University of Western Australia
- Message-ID: <PRYE.96Apr5014222@cyllene.uwa.edu.au>
- References: <DLABELL.96Apr4021045@columbia.pcs.cnu.edu>
- NNTP-Posting-Host: ub239.dialup.uwa.edu.au
- In-reply-to: dlabell@pcs.cnu.edu's message of 04 Apr 1996 07:10:45 GMT
- X-Mailer: GNU Emacs 19.28
-
- >>>>> "Daniel" == Daniel LaBell <dlabell@pcs.cnu.edu> writes:
-
- Daniel> get right to the problem. How do I cast a pointer to a
- Daniel> function pointer?
-
- Daniel> Here is trivial example that shows the problem.
-
- Daniel> void foo1(){ cout << " foo1 " << endl; }
- Daniel> void foo2(){ cout << " foo2 " << endl; }
- Daniel> void foo3(int x) { cout << " foo3, argument = " << x << endl; }
-
- Daniel> void do_a_foo( void (*fun)() ) { (*fun)(); }
- Daniel> void do_a_foo2(int x, void (*fun) (int )) { (*fun) ( x ); }
-
- Daniel> int main() {
- Daniel> do_a_foo ( foo1 );
- Daniel> do_a_foo ( foo2 );
- Daniel> do_a_foo2 ( 2, foo3 );
- Daniel> void * x=foo2;
- Daniel> do_a_foo (x); // I don't know the syntax to do this cast. :(
- Daniel> return 0; }
-
- Actually you do know the syntax for the cast. You've written it out
- in your function declarations. ;-)
- Instead of making x a void*, declare it as a function pointer.
- ie:
- void(*x)() = foo2;
- or void(*x)(int) = foo3; for a pointer to foo3.
-
- A typedef would make things less ugly.
- I'm sure this can be done in a much slicker object oriented C++ way.
-
- ANSI C doesn't allow the implicit conversion between void * and pointer-to-
- function.
-
- --
- Peter Rye prye@cyllene.uwa.edu.au, prye@ichr.uwa.edu.au
- Respiratory Research Fellow, Princess Margaret Hospital for Children
- Perth, Western Australia Ph: +61 (09) 340 8985, Fax: +61 (09) 388 2097
- ** Smoking areas in restaurants are like peeing areas in swimming pools. **
-